//
//  CTView.swift
//  elarc01
//
//  Created by elarc on 16/6/24.
//  Copyright © 2016年 elarc. All rights reserved.
//

import UIKit

class CTView: UIView {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)
        
//        1
        let context = UIGraphicsGetCurrentContext()
//        2
        CGContextSetTextMatrix(context, CGAffineTransformIdentity)
        CGContextTranslateCTM(context, 0, self.bounds.size.height)
        CGContextScaleCTM(context, 1.0, -1.0)
//        3
        let path = CGPathCreateMutable()
        let path1 = UIBezierPath(roundedRect: self.bounds , cornerRadius: self.bounds.size.width/2)
        CGPathAddRect(path, nil, self.bounds)
//        4
        let attrString = "流云在天边，行囊在眼前，有一条通往太阳的路无边又无沿,不是人人都能活的低调，可以低调的基础是随时都能高调,人生的冷暖取决于心灵的温度,绝口不提不是因为忘记，而是因为铭记,就算不快乐也不要皱眉，因为你永远不知道谁会爱上你的笑容"
        
        let mutableAttrStr = NSMutableAttributedString(string: attrString)
        mutableAttrStr.addAttributes([NSFontAttributeName:UIFont.systemFontOfSize(20),
            NSForegroundColorAttributeName:UIColor.redColor() ], range: NSMakeRange(0, 20))
        mutableAttrStr.addAttributes([NSFontAttributeName:UIFont.systemFontOfSize(13),NSUnderlineStyleAttributeName: 1 ], range: NSMakeRange(20,18))
        let framesetter = CTFramesetterCreateWithAttributedString(mutableAttrStr)
        let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, mutableAttrStr.length), path1.CGPath, nil)
        
//        5
        CTFrameDraw(frame,context!)
    }
    
    
}
